import cgi
import os
import urllib2
import re
from google.appengine.ext.webapp import template

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.api import urlfetch

stock_no_str = ['SHA:600778','SHA:600006','SHA:600133','SHE:000099','SHE:000033','SHE:000002','SHA:000001','SHA:600778']
#['SHA:601857','SHA:601398','SHA:600028','SHA:601988','SHA:601088','SHA:601628','SHA:600036','SHA:601318','SHA:601328','SHA:601601','SHA:600019','SHA:601919','SHA:601600','SHA:601006','SHA:601998','SHA:600050','SHA:600030','SHA:601898','SHA:600519','SHA:600016','SHA:601166','SHE:000002','SHA:600000','SHA:600018','SHA:600900','SHA:600837','SHA:601390','SHA:600005','SHA:601186','000898','SHA:601111','SHA:601991','SHE:000858']

class MainPage(webapp.RequestHandler):
    def get_chart_url_for_history( self,no_str ):
        return "http://www.google.cn/finance/historical?q=" + no_str
    def get_chart_url_for_month( self,no_str ):
        return "http://www.google.cn/m?site=finance&client=ms-aff-ucweb&q=" + no_str + "&output=xhtml&channel=homepage&page=chartpage%2Conemonth%2C" + no_str
    def get_chart_url_for_week( self,no_str ):
        return "http://www.google.cn/m?site=finance&client=ms-aff-ucweb&q=" + no_str + "&output=xhtml&channel=homepage&page=chartpage%2Coneweek%2C" + no_str
    def get_chart_url_for_day( self,no_str ):
        return "http://www.google.cn/m?site=finance&client=ms-aff-ucweb&output=xhtml&channel=homepage&q=" + no_str

    def get_chart_string( self, f, no ):
        str = no[-6:]
        #print cgi.escape( f )
        lines = re.findall(r'http://www\.(\S*)', f, re.M)
        line = ''
        for l in lines:
            m = re.search( 'chd=s', l )
            if m:
                line = l
                break
        lines = re.findall(r's[%|:](\S*?)&', line, re.S)
        return lines[0]
    def get_chart_price( self, f, no ):
        str = no[-6:]
        lines = re.findall(r'[>|:](\d+)\.(\d+)', f, re.S)
        return lines[0][0] + '.' + lines[0][1]

    def show_type_has_p_or_d(self, show_type):
        if 'p' in show_type:
            return True
        if 'd' in show_type:
            return True

    def get(self):
        str = ''
        no_str_line = self.request.get('no','SHA:600006,SHA:600133')
        show_type = self.request.get('type','pdwmh')
        no_strs = no_str_line.split( ',' )
        #for no_str in stock_no_str :
        for no_str in no_strs :
            str += no_str
            str += '<br>'

            if self.show_type_has_p_or_d(show_type):
                url = self.get_chart_url_for_day( no_str )
                result = urlfetch.fetch( url )
                if result.status_code == 200:
                    if 'p' in show_type:
                        str += self.get_chart_price( result.content, no_str )
                        str += '<br>'
                    if 'd' in show_type:
                        str += self.get_chart_string( result.content, no_str )
                        str += '<br>'

            if 'w' in show_type:
                url = self.get_chart_url_for_week( no_str )
                result = urlfetch.fetch( url )
                if result.status_code == 200:
                    str += self.get_chart_string( result.content, no_str )
                    str += '<br>'
    
            if 'm' in show_type:
                url = self.get_chart_url_for_month( no_str )
                result = urlfetch.fetch( url )
                if result.status_code == 200:
                    str += self.get_chart_string( result.content, no_str )
                    str += '<br>'
    
            if 'h' in show_type:
                url = self.get_chart_url_for_history( no_str )
                result = urlfetch.fetch( url )
                if result.status_code == 200:
                    str += self.get_chart_string( result.content, no_str )
                    str += '<br>'
            str += '<br>'

        self.response.out.write( str )
        

application = webapp.WSGIApplication(
        [('/test', MainPage)
            ,('/testsign', MainPage)
            ],
        debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

